<template>
  <div style="width: 100%; height: 100%">
    <div class="global_spinning" v-if="show_loading">
      <img src="../assets/spinningCSXR.gif" alt v-if="systemTitle === '橙事新人'" />
      <img src="../assets/spinningFjqy.gif" alt v-else-if="systemTitle === '星企途'" />
      <img src="../assets/spinningWry.png" alt v-else-if="systemTitle === '卫人云'" />
      <img src="../assets/spinningCSXR.gif" alt v-else />
    </div>
  </div>
</template>
<script>
import { mapState, mapGetters } from "vuex";
import adminMenuMap from "@/router/adminMenuMap";
import { getUrlKey } from "@/utils";
export default {
  name: "",
  components: {},
  mixins: [],
  props: {},
  data() {
    return {
      show_loading: true
    };
  },
  computed: {
    ...mapState(["settings", "auth", "consolePermission"]),
    ...mapGetters(["permissions_menu", "permissions_handle", "permissions_menu_urls"]),
    systemTitle() {
      if (window.location.host === "hr.csxr.cn") {
        return "橙事新人";
      } else if (window.location.host === "hr.qiyeyc.com" || window.location.host.includes("xingqitu.com")) {
        return "星企途";
      } else if (window.location.host.includes("sdwry.com")) {
        return "卫人云";
      } else {
        return "橙事新人";
      }
    },
    settingsLoaded() {
      return this.settings.loaded;
    },
    authLoaded() {
      return this.auth.loaded;
    },
    token() {
      return this.auth.token;
    },
    isReady() {
      return !this.show_loading && this.authLoaded && this.token;
    }
  },
  watch: {
    settingsLoaded: {
      handler(newVal) {
        if (process.env.VUE_APP_DYNAMIC_CONFIG === "true") {
          if (newVal) {
            this.$nextTick(() => {
              setTimeout(() => {
                this.show_loading = false;
              }, 1000);
            });
          }
        } else {
          this.$nextTick(() => {
            setTimeout(() => {
              this.show_loading = false;
            }, 1000);
          });
        }
      },
      immediate: true
    },
    show_loading(newVal) {
      if (!newVal) {
        if (!this.token) {
          this.$router.push("/auth/login");
        }
      }
    },
    isReady(newVal) {
      if (newVal) {
        this.goPage();
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    async goPage() {
      const redirect = getUrlKey("redirect");
      if (!redirect) {
        // 是否有操作后台权限，根据 permissions_menu 来判断
        if (this.permissions_menu && this.permissions_menu.length > 0) {
          this.$router.push("/dashboard");
        } else {
          // 如果没有操作后台权限，需要判断管理后台权限，根据 this.permissions_handle 包含 hr
          let flag = false;
          let startPer = "";
          if (this.permissions_handle && this.permissions_handle.length > 0) {
            for (let i = 0; i < this.permissions_handle.length; i++) {
              const menu = this.permissions_handle[i];
              if (menu.split(":")[0] === "hr") {
                flag = true;
                startPer = menu;
                break;
              }
            }
          }
          if (flag) {
            if (this.permissions_handle.includes("hr:workbench:workStation")) {
              startPer = "hr:workbench:workStation";
            }
            this.$store.commit("hr/setAdminNavState", adminMenuMap[startPer]["belong"]);
            this.$router.push(adminMenuMap[startPer]["router"]);
          } else {
            if (this.$hasPermissionFirst("insuranceAddService")) {
              sessionStorage.setItem("shangbaoPageState", "customer");
              this.$router.push({
                path: "/shangyebaoxian"
              });
              return false;
            } else {
              if (this.auth && this.auth.permissions && this.auth.permissions.moduleList && this.auth.permissions.moduleList.length > 0) {
                if (this.auth.permissions.moduleList.filter((item) => item.moduleType === "insurance").length > 0) {
                  sessionStorage.setItem("shangbaoPageState", "company");
                  this.$router.push({
                    path: "/shangyebaoxian"
                  });
                  return false;
                }
                if (this.auth.permissions.moduleList.filter((item) => item.moduleType === "supper").length > 0) {
                  if (this.consolePermission && this.consolePermission.menuList && this.consolePermission.menuList.length > 0) {
                    this.$router.push("/console");
                    return false;
                  }
                }
              }
              // 如果都没有，就跳转到空权限页面
              this.$router.push("/empty");
            }
          }
        }
      } else {
        if (redirect.includes("shangyebaoxian")) {
          if (this.$hasPermissionFirst("insuranceAddService")) {
            sessionStorage.setItem("shangbaoPageState", "customer");
            this.$router.push({
              path: "/shangyebaoxian"
            });
          } else {
            this.$router.push("/empty");
          }
        } else if (redirect.includes("taxCalculate")) {
          if (this.$hasPermissionSecond("zhinengsuanshui:koujiaoyiwurenguanli")) {
            this.$router.push({ path: "/taxCalculate/management" });
          } else if (this.$hasPermissionSecond("zhinengsuanshui:baoshuirenyuanku")) {
            this.$router.push({ path: "/taxCalculate/people" });
          } else if (this.$hasPermissionSecond("zhinengsuanshui:renyuanbaosong.daibaosong")) {
            this.$router.push({ path: "/taxCalculate/waiting" });
          } else if (this.$hasPermissionSecond("zhinengsuanshui:renyuanbaosong.yibaosong")) {
            this.$router.push({ path: "/taxCalculate/report" });
          } else if (this.$hasPermissionSecond("zhinengsuanshui:renyuanbaosong.tingzhibaosong")) {
            this.$router.push({ path: "/taxCalculate/stop" });
          } else if (this.$hasPermissionSecond("hinengsuanshui:geshuijisuan")) {
            this.$router.push({ path: "/taxCalculate/calculate" });
          } else {
            this.$router.push("/empty");
          }
        } else if (redirect.includes("recruitment")) {
          if (this.$hasPermission("recruitment:roverview:query")) {
            this.$router.push({ path: "/recruitment/overview" });
          } else if (this.$hasPermission("recruitment:management:query")) {
            this.$router.push({ path: "/recruitment/management" });
          } else if (this.$hasPermission("recruitment:people:query")) {
            this.$router.push({ path: "/recruitment/people" });
          } else if (this.$hasPermission("recruitment:economic:query")) {
            this.$router.push({ path: "/recruitment/economic" });
          } else if (this.$hasPermission("recruitment:rpush:query")) {
            this.$router.push({ path: "/recruitment/push" });
          } else if (this.$hasPermission("recruitment:laborUnit:query")) {
            this.$router.push({ path: "/recruitment/laborUnit" });
          } else if (this.$hasPermission("recruitment:talent:query")) {
            this.$router.push({ path: "/recruitment/talent" });
          } else if (this.$hasPermission("recruitment:zhinengtuijian:query")) {
            this.$router.push({ path: "/recruitment/talentZhineng" });
          } else {
            this.$router.push("/empty");
          }
        } else if (redirect.includes("staff")) {
          if (this.permissions_menu_urls.includes("/staff/employee")) {
            this.$router.push({ path: "/staff/employee" });
          } else if (this.permissions_menu_urls.includes("/staff/manger")) {
            this.$router.push({ path: "/staff/manger" });
          } else if (this.permissions_menu_urls.includes("/staff/edit")) {
            this.$router.push({ path: "/staff/edit" });
          } else {
            this.$router.push("/empty");
          }
        } else if (redirect.includes("contract")) {
          if (this.permissions_menu_urls.includes("/contract/elecontract")) {
            this.$router.push({ path: "/contract/elecontract" });
          } else if (this.permissions_menu_urls.includes("/contract/type")) {
            this.$router.push({ path: "/contract/type" });
          } else if (this.permissions_menu_urls.includes("/contract/stamp")) {
            this.$router.push({ path: "/contract/stamp" });
          } else {
            this.$router.push("/empty");
          }
        } else {
          this.$router.push(redirect);
        }
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.global_spinning {
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
}

.global_spinning img {
  width: 100px;
}
</style>
